<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得结果中指定字段的字段名</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-field-len.html">« mysql_field_len</a></li>
      <li style="float: right;"><a href="function.mysql-field-seek.html">mysql_field_seek »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>取得结果中指定字段的字段名</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-field-name" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_field_name</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_field_name</span> &mdash; <span class="dc-title">
     取得结果中指定字段的字段名
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.mysql-field-name-description">
  <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>mysql_field_name</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$field_index</code></span>): <span class="type">string</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_field_name()</strong></span>
     返回指定字段索引的字段名。<code class="parameter">result</code>
     必须是一个合法的结果标识符，<code class="parameter">field_index</code>
     是该字段的数字偏移量。
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      <code class="parameter">field_index</code> 从 0 开始。
     </p>
     <p class="para">
      例如，第三个字段的索引值其实是 2，第四个字段的索引值是 3，以此类推。
     </p>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数返回的字段名<em class="emphasis">大小写敏感</em>。</span></p></blockquote>
    <p class="para">
     <div class="example" id="example-1759">
      <p><strong>示例 #1 <span class="function"><strong>mysql_field_name()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;The&nbsp;users&nbsp;table&nbsp;consists&nbsp;of&nbsp;three&nbsp;fields:<br />&nbsp;*&nbsp;&nbsp;&nbsp;user_id<br />&nbsp;*&nbsp;&nbsp;&nbsp;username<br />&nbsp;*&nbsp;&nbsp;&nbsp;password.<br />&nbsp;*/<br /></span><span style="color: #0000BB">$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"mysql_user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"mysql_password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dbname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbname</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;set&nbsp;</span><span style="color: #0000BB">$dbname</span><span style="color: #DD0000">:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;*&nbsp;from&nbsp;users"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">mysql_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">mysql_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>
       以上例子将产生如下输出：
      </p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
user_id
password
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     为向下兼容仍然可以使用 <span class="function"><strong>mysql_fieldname()</strong></span>，但反对这样做。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-field-name-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">result</code></dt>
<dd>
<p class="para"><span class="type">resource</span>
型的结果集。此结果集来自对 <span class="function"><a href="function.mysql-query.html" class="function">mysql_query()</a></span>
的调用。</p></dd>

    <dt>

<code class="parameter">field_offset</code></dt>
<dd>
<p class="para">数值型字段偏移量。
<code class="parameter">field_offset</code> 从 <code class="literal">0</code> 开始。如果
<code class="parameter">field_offset</code> 不存在，则会发出一个
<strong><code>E_WARNING</code></strong> 级别的错误</p></dd>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-field-name-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The name of the specified field index on success 或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-field-name-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1760">
    <p><strong>示例 #2 <span class="function"><strong>mysql_field_name()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;The&nbsp;users&nbsp;table&nbsp;consists&nbsp;of&nbsp;three&nbsp;fields:<br />&nbsp;*&nbsp;&nbsp;&nbsp;user_id<br />&nbsp;*&nbsp;&nbsp;&nbsp;username<br />&nbsp;*&nbsp;&nbsp;&nbsp;password.<br />&nbsp;*/<br /></span><span style="color: #0000BB">$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Could&nbsp;not&nbsp;connect&nbsp;to&nbsp;MySQL&nbsp;server:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">$dbname&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mydb'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$db_selected&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbname</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$db_selected</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Could&nbsp;not&nbsp;set&nbsp;</span><span style="color: #0000BB">$dbname</span><span style="color: #DD0000">:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'select&nbsp;*&nbsp;from&nbsp;users'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">mysql_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">mysql_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
user_id
password
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-field-name-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数返回的字段名<em class="emphasis">大小写敏感</em>。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    为了向下兼容，可以使用下列已废弃的别名：
    <span class="function"><strong>mysql_fieldname()</strong></span>
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-field-name-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mysql-field-type.html" class="function" rel="rdfs-seeAlso">mysql_field_type()</a> - 取得结果集中指定字段的类型</span></li>
    <li class="member"><span class="function"><a href="function.mysql-field-len.html" class="function" rel="rdfs-seeAlso">mysql_field_len()</a> - 返回指定字段的长度</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>